<template>
  <div class="login-form-wrap">
    <div class="login-form-title">注册</div>
    <a-form :model="loginForm" class="login-form" @finish="onFinish">
      <a-form-item
        label="姓名"
        name="username"
        :rules="[{ required: true, message: '请输入姓名' }]"
        class="login-form-item"
      >
        <a-input v-model:value="loginForm.username">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        label="密码"
        name="password"
        :rules="[{ required: true, message: '请输入密码' }]"
        class="login-form-item"
      >
        <a-input-password v-model:value="loginForm.password">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>
      <a-form-item>
        <div class="site-form-button-wrap">
          <a-button type="primary" html-type="submit" class="login-form-button"> 注册 </a-button>
          <a-button type="primary" @click="emit('login')"> 返回 </a-button>
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup>
import { reactive, computed } from 'vue'
import { register } from '@/api/user'
import { message } from 'ant-design-vue'
const emit = defineEmits(['login'])
const loginForm = reactive({
  username: '',
  password: ''
})
const disabled = computed(() => {
  return !(loginForm.username && loginForm.password)
})

const clearForm = () => {
  loginForm.username = ''
  loginForm.password = ''
}

const onFinish = (values) => {
  register(values)
    .then(() => {
      message.success('注册成功')
      clearForm()
      emit('login')
    })
    .catch(() => {
      message.error('注册失败')
    })
}
</script>

<style scoped lang="scss">
.login-form-wrap {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotateY(180deg);
  width: 400px;
  height: 300px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  text-align: center;
  padding: 20px 10px;
  transform-origin: top center;
  transition: all 1s ease-in-out;
  opacity: 0;
  z-index: 0;
  .login-form-title {
    font-size: 30px;
    margin-bottom: 30px;
  }

  .login-form-item {
    margin-bottom: 30px;
  }
  .site-form-button-wrap {
    width: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 0 5px;
  }
}
</style>
